package yxy.cherry.paycenter.service;

import javax.servlet.http.HttpServlet;

import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import yxy.apple.logger.Logger;
import yxy.cherry.paycenter.Configuration;
import yxy.cherry.paycenter.servlet.channel.huawei.Servlet_huawei;
import yxy.cherry.paycenter.servlet.channel.huawei.Servlet_huawei2;
import yxy.cherry.paycenter.servlet.channel.huawei_quick.Servlet_huawei_quick;
import yxy.cherry.paycenter.servlet.channel.jh.Servlet_jh;
import yxy.cherry.paycenter.servlet.channel.jhh5.Servlet_jhh5;
import yxy.cherry.paycenter.servlet.channel.shengye.Servlet_ShengYe;
import yxy.cherry.paycenter.servlet.channel.vivo_quick.Servlet_vivo_quick;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinAndroid;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinIos;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinPrePayAndroid;
import yxy.cherry.paycenter.servlet.channel.weixin.Servlet_WeiXinPrePayIos;

public class SdkHttpService {
	// http 服务器
	private Server httpServer;
	private static SdkHttpService instance;
	private ServletContextHandler context;
	private int port;

	public static SdkHttpService getInstance() {
		if (instance == null) {
			instance = new SdkHttpService();
		}
		return instance;
	}
	
	static final Logger logger = Logger.create(SdkHttpService.class);
	
	//http 启动
	public void httpinit(Configuration configuration) {
		httpServer = new Server();
		HttpConfiguration http_config = new HttpConfiguration();
		final ServerConnector connector = new ServerConnector(httpServer, new HttpConnectionFactory(http_config));
		connector.setPort(configuration.HttpProt);
		httpServer.addConnector(connector);

		logger.info("http支付回调端口：" + configuration.HttpProt);
		
		// ----------------------
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath("/");
		httpServer.setHandler(context);
		initServlet(context);
		
		try {
			System.out.println("*********sdk service http begin:" + configuration.HttpProt);
			httpServer.start();
			System.out.println("*********sdk service http end:" + configuration.HttpProt);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initServlet(ServletContextHandler context) {
		Servlet_jh jh = new Servlet_jh();
		context.addServlet(new ServletHolder(jh), jh.getUrl());
		logger.info("jh :" + jh.getUrl());
		
		Servlet_huawei huawei = new Servlet_huawei();
		context.addServlet(new ServletHolder(huawei), huawei.getUrl());
		logger.info("huawei :" + huawei.getUrl());
		Servlet_huawei2 huawei2 = new Servlet_huawei2();
		context.addServlet(new ServletHolder(huawei2), huawei2.getUrl());
		logger.info("huawei2 :" + huawei2.getUrl());

		//华为快游戏
		Servlet_huawei_quick huawei_quick = new Servlet_huawei_quick();
		context.addServlet(new ServletHolder(huawei_quick), huawei_quick.getUrl());
		logger.info("huawei_quick :" + huawei_quick.getUrl());

		//vivo快游戏
		Servlet_vivo_quick vivo_quick = new Servlet_vivo_quick();
		context.addServlet(new ServletHolder(vivo_quick), vivo_quick.getUrl());
		logger.info("vivo_quick :" + vivo_quick.getUrl());
		
//		Servlet_WeiXin weixin_ios = new Servlet_WeiXin();
//		context.addServlet(new ServletHolder(weixin_ios), weixin_ios.getUrl());
		Servlet_WeiXinPrePayIos weixin_iosprepay = new Servlet_WeiXinPrePayIos();
		context.addServlet(new ServletHolder(weixin_iosprepay), weixin_iosprepay.getUrl());
		logger.info("weixin_iosprepay :" + weixin_iosprepay.getUrl());
		
		Servlet_WeiXinPrePayAndroid weixin_androidprepay = new Servlet_WeiXinPrePayAndroid();
		context.addServlet(new ServletHolder(weixin_androidprepay), weixin_androidprepay.getUrl());
		logger.info("weixin_androidprepay :" + weixin_androidprepay.getUrl());
		
		Servlet_WeiXinIos weixin_ios = new Servlet_WeiXinIos();
		context.addServlet(new ServletHolder(weixin_ios), weixin_ios.getUrl());
		logger.info("weixin_ios :" + weixin_ios.getUrl());
		
		Servlet_WeiXinAndroid weixin_android = new Servlet_WeiXinAndroid();
		context.addServlet(new ServletHolder(weixin_android), weixin_android.getUrl());
		logger.info("weixin_android :" + weixin_android.getUrl());
		
		Servlet_ShengYe shengye = new Servlet_ShengYe();
		context.addServlet(new ServletHolder(shengye), shengye.getUrl());
		logger.info("shengye :" + shengye.getUrl());
		
		Servlet_jhh5 jhh5 = new Servlet_jhh5();
		context.addServlet(new ServletHolder(jhh5), jhh5.getUrl());
		logger.info("jhh5 :" + jhh5.getUrl());
	}


	public void addServlet(String path, HttpServlet servlet) {
		context.addServlet(new ServletHolder(servlet), path);
	}

	public void start() {
		try {
			httpServer.start();
			System.out.println("*********gm service start:" + port);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stop() {
		if (httpServer != null) {
			try {
				httpServer.stop();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
